import logging.config

from flask import Flask
from flask_migrate import Migrate

from application.models import db
from application.job import scheduler


def register_extensions(app):
    db.init_app(app)
    Migrate(app, db)
    return


def register_blueprints(app):
    from .handlers import front_handler
    app.register_blueprint(front_handler)


def register_apis(app):
    from .apis import user_api
    app.register_blueprint(user_api)


def init_config(app):
    from .settings import LOGGING_CONF
    logging.config.dictConfig(LOGGING_CONF)
    app.config.from_envvar('FLASK_DEMO_APPLICATION_SETTINGS')


def create_app():
    app = Flask(__name__)
    init_config(app)
    register_extensions(app)
    register_blueprints(app)
    register_apis(app)
    scheduler.start()
    app.logger.info("应用程序已启动！")
    return app
